import { ipcRenderer } from 'electron';
import uuid from 'uuid';
import { IDesktopSendMsg } from '../../../interfaces';

/**
 * 调用服务
 * @param spaceid 系统参数spaceid
 * @param type 服务类型
 * @param msg 参数
 */
export default function service(spaceid: string, type: string, msg: any) {
	return new Promise<any>((resolve, reject) => {
		const reply = uuid();
		ipcRenderer.once(reply, (_event: any, arg: {
			data?: any;
			errMsg?: string;
		}) => {
			if (arg.errMsg) {
				console.error(`--调用服务${type}失败--错误信息---`, arg.errMsg);
				reject(arg.errMsg);
			} else {
				resolve(arg.data);
			}
		});
		ipcRenderer.send('msg', {
			msg,
			reply,
			spaceid,
			type
		} as IDesktopSendMsg);
	});
}
